home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Plus / Multimedia Plus with ClearVue Version 10-94 (Knowledge Media Inc.).ISO / win3x / scr_grbr / wndgrab / wingrab.c < prev    next >
Text File  |  1991-01-16  |  9KB  |  211 lines

  1. /********************************************************************/
  2. //                                                                   / 
  3. //   File:          WinGrab.c                                        /
  4. //   Function:      Whole Screen Grabber for Window 3.0              /
  5. //   Description:   Grab whole picture from screen into clipboard.   /
  6. //                  The data formats are BITMAP and COLOR PALETTES.  /
  7. //   Programmer:    Achen ( EE Graduate Student of UF )              /
  8. //   Date:          Jan 14, 1991                                     /
  9. //   Note:          All rights are free.                             /
  10. //                                                                   /
  11. /********************************************************************/
  12.  
  13. #define  NOCOMM                                 // disable some definitions
  14. #define  DEFAULT_PALETTE_SIZE 20                // default value.
  15.  
  16. #include <windows.h>
  17.  
  18. /****************************************/
  19. //                                       /
  20. //   Functions defined in this file      /
  21. //                                       /
  22. /****************************************/
  23. DWORD FAR PASCAL CheckGrabHotKey(WORD nCode, WORD wParam , LONG lParam);
  24. BOOL  FAR PASCAL LibMain();
  25. VOID      PASCAL GrabToClipBoardByBitmap();
  26. VOID      PASCAL BitmapToDIB();
  27.  
  28. FARPROC lpfnOldKbdHook;                         // old Keyboard function hook
  29.  
  30. /***************************************************************************/
  31. //  LibMain()                                                               /
  32. //                                                                          /
  33. //  Function: Initialize the keyboard hook.                                 /
  34. //                                                                          /
  35. /***************************************************************************/
  36. BOOL FAR PASCAL LibMain()
  37. {
  38.     if( (lpfnOldKbdHook = SetWindowsHook(WH_KEYBOARD, (FARPROC)CheckGrabHotKey) ) )
  39.         MessageBox( NULL,"Window Screen Grabber installed O.K.\n\n"
  40.            " Copy whole screen to clipboard by pressing : \n\n"
  41.            " F8 = using BITMAP and PALETTE formats.\n"
  42.            " F9 = using DIB (Device Indenpent Bitmap) format.",
  43.            "WinGrab Ver. 1.0 by Achen", MB_OK );
  44.     else
  45.         MessageBox( NULL,"Screen Grabber failed to install.", "WinGrab", MB_OK );
  46.  
  47.     return (int)lpfnOldKbdHook ;
  48. }
  49.  
  50.  
  51. /***************************************************************************/
  52. //  CheckGrabHotKey                                                         /
  53. //                                                                          /
  54. //  Function:  Keyboard hook routine to check Hot Keys                      /
  55. //                                                                          /
  56. /***************************************************************************/
  57. DWORD FAR PASCAL CheckGrabHotKey(WORD nCode, WORD wParam , LONG lParam)
  58. {
  59.     switch( nCode ) {
  60.         case HC_ACTION:
  61.             if( lParam & 0x80000000L )          // if key is being released ?
  62.                 switch (wParam) {
  63.                     case VK_F8:
  64.                     case VK_F9:                 // Grab the Whole Screen. 
  65.                         if( OpenClipboard( NULL ) ) {
  66.                             EmptyClipboard();
  67.                             CloseClipboard();
  68.                             GrabToClipBoardByBitmap();
  69.                             if( wParam == VK_F9 )
  70.                                 BitmapToDIB();
  71.                         }
  72.                         break;
  73.                 }
  74.         default:
  75.         return DefHookProc( nCode, wParam, lParam, &lpfnOldKbdHook );
  76.     }
  77. }
  78.  
  79.  
  80. /****************************************************************************/
  81. //  GrabToClipBoardByBitmap                                                  /
  82. //                                                                           /
  83. //  Function: Grabs the screen picture into a bitmap and                     /
  84. //            a palette ( if supported ), then puts them into the clipboard  /
  85. //                                                                           /
  86. /****************************************************************************/
  87. VOID PASCAL GrabToClipBoardByBitmap()
  88. {
  89. HDC     hDC, hMemoryDC, hPalette, hOldBitmap, hBitmap;
  90. WORD    ScreenXlen, ScreenYlen, PaletteSize;
  91. LPLOGPALETTE lpPalette;
  92.  
  93.     // get the Device Content of whole screen.
  94.     if( !( hDC = CreateDC( "DISPLAY", NULL, NULL, NULL ) ) ) return;
  95.  
  96.     // get screen Width and Height.
  97.     ScreenXlen = GetDeviceCaps( hDC, HORZRES );
  98.     ScreenYlen = GetDeviceCaps( hDC, VERTRES );
  99.  
  100.     // Create a memory DC compatible with the current video device. 
  101.     if( !( hMemoryDC = CreateCompatibleDC( hDC ) ) ){
  102.         DeleteDC( hDC );
  103.         return;
  104.     }
  105.  
  106.     // Add color palettes to clipboard.
  107.     if( !(PaletteSize = GetDeviceCaps( hDC, SIZEPALETTE ) ) )
  108.          PaletteSize = DEFAULT_PALETTE_SIZE;
  109.     hPalette    = GlobalAlloc( GPTR, (LONG)PaletteSize * sizeof(PALETTEENTRY)+sizeof(LOGPALETTE));
  110.     lpPalette   = (LPLOGPALETTE) GlobalLock( hPalette );
  111.     lpPalette->palVersion    = 0x300;
  112.     lpPalette->palNumEntries = PaletteSize;
  113.     if( GetDeviceCaps( hDC, SIZEPALETTE ) )     // if system palette exists ?
  114.         GetSystemPaletteEntries( hDC, 0, PaletteSize,(LPPALETTEENTRY)lpPalette->palPalEntry );
  115.     else
  116.         GetPaletteEntries( GetStockObject(DEFAULT_PALETTE), 0, PaletteSize,(LPPALETTEENTRY)lpPalette->palPalEntry );
  117.  
  118.     // create a bitmap for the whole screen. 
  119.     if( (hBitmap = CreateCompatibleBitmap( hDC, ScreenXlen, ScreenYlen ) ) ){
  120.  
  121.         // Select the bitmap into memory DC. 
  122.         hOldBitmap = SelectObject( hMemoryDC, hBitmap );
  123.  
  124.         // copy the whole screen picture into this bitmap.
  125.         BitBlt( hMemoryDC, 0, 0, ScreenXlen, ScreenYlen, hDC, 0, 0, SRCCOPY);
  126.         hBitmap = SelectObject( hMemoryDC, hOldBitmap );
  127.  
  128.         if( OpenClipboard( NULL ) ){
  129.             EmptyClipboard();            // let Clipboard handles this Bitmap.
  130.             if( !SetClipboardData( CF_BITMAP, hBitmap ) ) 
  131.                 DeleteObject( hBitmap );
  132.                                          // let Clipboard handle this Palette.
  133.             SetClipboardData( CF_PALETTE, CreatePalette(lpPalette) );
  134.             CloseClipboard();
  135.         }else{
  136.             DeleteObject( hBitmap );     // open clipboard failed.
  137.             MessageBox( NULL,"Can't Open Clipboard !!!", "WinGrab", MB_OK);
  138.         }
  139.     }else{ 
  140.         MessageBox( NULL,
  141.            "Screen Grabber failed, Need more memory ?", "WinGrab", MB_OK );
  142.     }
  143.  
  144.     GlobalUnlock( hPalette  );
  145.     GlobalFree  ( hPalette  );
  146.     DeleteDC    ( hMemoryDC );
  147.     DeleteDC    ( hDC       );
  148. }
  149.  
  150.  
  151. /****************************************************************************/
  152. //  BitmapToDIB                                                              /
  153. //                                                                           /
  154. //  Function: Transform the Bitmap and Palette in ClipBoard to DIB format.   /
  155. //            and destroy Bitmap and Palette.                                /
  156. //                                                                           /
  157. /****************************************************************************/
  158. VOID PASCAL BitmapToDIB()
  159. {
  160. HDC     hDC, hDIB, hPalette, hBitmap, hOldPalette;
  161. WORD    ScreenXlen, ScreenYlen, DIBits, PaletteSize, InfoLen;
  162. LPBITMAPINFO lpDIB;
  163.  
  164.     // open ClipBoard and read BITMAP and PALETTE.
  165.     if( !OpenClipboard( NULL ) ) return; 
  166.     hBitmap = GetClipboardData( CF_BITMAP  );
  167.     hPalette= GetClipboardData( CF_PALETTE );
  168.     if( !hBitmap || !hPalette ){
  169.         CloseClipboard();
  170.         return;
  171.     }
  172.     
  173.     // Get DC and some attributes of the Vedio Device.
  174.     hDC         = GetDC( NULL );
  175.     ScreenXlen  = GetDeviceCaps( hDC, HORZRES );
  176.     ScreenYlen  = GetDeviceCaps( hDC, VERTRES );
  177.     DIBits      = GetDeviceCaps( hDC, PLANES) * GetDeviceCaps(hDC, BITSPIXEL);
  178.     PaletteSize = 1 << DIBits;
  179.     InfoLen     = sizeof(BITMAPINFOHEADER) + PaletteSize * sizeof( RGBQUAD );
  180.  
  181.     // allocate enough memory for DIB.
  182.     if( (hDIB = GlobalAlloc( GPTR, (long)ScreenXlen*ScreenYlen*DIBits/8+InfoLen ))){
  183.         lpDIB = (LPBITMAPINFO)GlobalLock( hDIB );
  184.  
  185.         // fill up DIB information structure. 
  186.         lpDIB->bmiHeader.biSize          =  (long) sizeof(BITMAPINFOHEADER);
  187.         lpDIB->bmiHeader.biWidth         =  (long) ScreenXlen;
  188.         lpDIB->bmiHeader.biHeight        =  (long) ScreenYlen;
  189.         lpDIB->bmiHeader.biPlanes        =  1;
  190.         lpDIB->bmiHeader.biBitCount      =  DIBits;
  191.         lpDIB->bmiHeader.biCompression   =  BI_RGB;
  192.  
  193.         // select and realize PALETTE.
  194.         hOldPalette = SelectPalette( hDC, hPalette, 0 );
  195.         RealizePalette( hDC );
  196.  
  197.         // Get DIB data from BITMAP.
  198.         GetDIBits( hDC, hBitmap, 0, ScreenYlen, (LPSTR)lpDIB+InfoLen, lpDIB, DIB_RGB_COLORS);
  199.         SelectPalette( hDC, hOldPalette, 0);
  200.  
  201.         // set DIB to ClipBoard.
  202.         EmptyClipboard();
  203.         GlobalUnlock( hDIB );               // let ClipBoard handle DIB.
  204.         if( !SetClipboardData( CF_DIB, hDIB ) )
  205.             GlobalFree( hDIB );
  206.     }
  207.  
  208.     CloseClipboard();
  209.     ReleaseDC( NULL, hDC );
  210. }
  211.